package com.wulaobo.transformer;

import com.wulaobo.pojo.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @Author wuwenbo
 * @Date 2024/5/17 10:31
 * @ClassName: MapDemo
 * @Description: TODO
 */
public class MapDemo {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

//        env.setParallelism(1);

        DataStreamSource<WaterSensor> stream = env.fromElements(
                new WaterSensor("sensor_1", 1, 1),
                new WaterSensor("sensor_2", 2, 2)
        );

        // 方式一：传入匿名类，实现MapFunction
//        stream.map(new MapFunction<WaterSensor, String>() {
//            @Override
//            public String map(WaterSensor e) throws Exception {
//                return e.getId();
//            }
//        }).print();

//        stream.map(waterSensor -> waterSensor.getId()).returns(Types.STRING).print();
//        stream.map(WaterSensor::getId).print();

        // 方式二：传入MapFunction的实现类
         stream.map(new UserMap()).print();

        env.execute();
    }

    public static class UserMap implements MapFunction<WaterSensor,
            String> {
        @Override
        public String map(WaterSensor e) throws Exception {
            return e.id;
        }
    }

}
